package cn.com.user.controller;

import java.io.UnsupportedEncodingException;
import java.lang.reflect.InvocationTargetException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.sword.wechat4j.oauth.OAuthException;
import org.sword.wechat4j.oauth.OAuthManager;
import org.sword.wechat4j.oauth.protocol.get_access_token.GetAccessTokenRequest;
import org.sword.wechat4j.oauth.protocol.get_access_token.GetAccessTokenResponse;
import org.sword.wechat4j.user.UserManager;

import com.cabinet.service.IUserService;
import com.cabinet.web.entity.User;

import cn.com.easy.utils.CookieUtils;




@Controller
@RequestMapping("/wechat")
public class AutoLoginController {
	
	@Autowired
	private IUserService iUserService;
	
	@RequestMapping("/login")
	public String AutoLogin(HttpServletRequest request, HttpServletResponse response,HttpSession session) throws Exception{
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		String code = request.getParameter("code");
		//String type = request.getParameter("type");
		
		//同步远程调用
		GetAccessTokenResponse accessToken=null;
			try {
				accessToken=OAuthManager.getAccessToken(new GetAccessTokenRequest(code));
				
				if(accessToken==null){
					return "/error/autoLoginERROR";
				}else{
					//获取用户的Openid
					String openId=accessToken.getOpenid();
					System.out.println("openid:"+openId);
					//session.setAttribute("openId", openId);
					
					User user = iUserService.getUserByOpenid(openId);
					System.out.println("123");
					if(user == null){
						UserManager um = new UserManager();
						org.sword.wechat4j.user.User u = um.getUserInfo(openId);
						User usersave = new User();
						usersave.setOpenId(u.getOpenId());//new String(u.getNickName().getBytes("ISO-8859-1"),"utf-8")
						usersave.setuName(new String(u.getNickName().getBytes("ISO-8859-1"),"utf-8"));
						String address = new String(u.getCountry().getBytes("ISO-8859-1"),"utf-8")+
										 new String(u.getProvince().getBytes("ISO-8859-1"),"utf-8")+
										 new String(u.getCity().getBytes("ISO-8859-1"),"utf-8");
						usersave.setuAddress(address);
						usersave.setuPhoto(u.getHeadimgUrl());
						if(u.getSex() == 1){
							usersave.setuSex("男");
						}else{
							if(u.getSex() == 2){
								usersave.setuSex("女");
							}
						}
						
						iUserService.addUser(usersave);
						System.out.println("--------"+usersave.getId());
						CookieUtils.setCookie(request, response, "userid",usersave.getId().toString());
					}else{
						CookieUtils.setCookie(request, response, "userid",user.getId().toString());
						//System.out.println(user);
					}
				}
				} catch (OAuthException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}	
			//return "redirect:"+CookieUtils.getCookieRedirectUrl(request);
			return "redirect:/";
	}
	
	
	@RequestMapping("/test")
	public String test(HttpServletRequest request, HttpServletResponse response) throws Exception{
		User user = iUserService.getUserByOpenid("ockRIv2n27nqhE7zyH-f9U3KtU9w");
		CookieUtils.setCookie(request, response, "userid",user.getId().toString());
		return "redirect:/index/120170412145237";
	}
	
	@RequestMapping("/QR/test")
	public String QRtest(HttpServletRequest request, HttpServletResponse response) throws Exception{
		User user = iUserService.getUserByOpenid("ockRIv2n27nqhE7zyH-f9U3KtU9w");
		CookieUtils.setCookie(request, response, "userid",user.getId().toString());
		return "redirect:/";
	}

}
